using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Security;
using System.Configuration;


	/// <summary>
	/// Summary description for SecretForm.
	/// </summary>
	public partial class Imageform:Page 
	{
		protected System.Web.UI.WebControls.PlaceHolder plcDataGrids;
		
		
		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void btnSubmit_Click(object sender, System.EventArgs e)
		{
			if(IsValid)
			{
                using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DataConnectionString"].ConnectionString))
				{
					try
					{				
						using(SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(txtQuery.Text, sqlConnection))
						{
							sqlDataAdapter.SelectCommand.CommandType = CommandType.Text;
							sqlConnection.Open();
							DataSet dataSet = new DataSet();
							sqlDataAdapter.Fill(dataSet);
							if(dataSet.Tables.Count>0)
							{
                                plcGrid.Controls.Clear();
                                foreach(DataTable dt in dataSet.Tables )
                                {
                                    DataGrid dg = new DataGrid();
                                    dg.DataSource = dt;
                                    dg.DataBind();
                                    plcGrid.Controls.Add(dg);
                                    Label lb = new Label();
                                    lb.Text = string.Format("<span class='success'>Total Record Found : {0}</span>", dt.Rows.Count);
                                    plcGrid.Controls.Add(lb);
                                }
        					}
							else
							{							
								lMessage.Text="<span class='success'>Query Executed successfully</span>";
							}
						}
					}
					catch(Exception ex)
					{
						lMessage.Text = string.Format("<span class='error'>{0}</span>",ex.GetBaseException());
					}
				}
			}
		}
	}

